package org.itboys.store.service;

import com.google.common.collect.Maps;
import org.itboys.store.dto.ProductDetail;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.store.cache.ProductDetailLoader;
import org.itboys.store.entity.CarItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by huml on 2016/5/19.
 * 购物车业务
 */
@Service
public class StoreCarItemService extends BaseService<CarItem, Long> {

    private static final long serialVersionUID = 4206939863089464373L;

    @Resource(name = "productDS")
    private MongoDataSource carItemDataSource;
    @Resource(name = "storeProductDetailLoader")
    private ProductDetailLoader productDetailLoader;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return carItemDataSource;
    }

    /**
     * 添加产品到购物车
     * @param memberId
     * @param productId
     * @param num
     */
    public CarItem addCarItem(Long memberId, Long productId, int num){
        //首先要判断这个会员的购物车里有没有这个产品,如果有,数量加上去,如果没有,新增一条数据
        CarItem item = getByMemberIdAndProductId(memberId, productId);
        if(item == null){
            item = new CarItem();
            ProductDetail detail = productDetailLoader.get(productId);
            item.setMemberId(memberId);
            item.setProductId(productId);
            item.setProductName(detail.getTitle());
            item.setProductImage(detail.getImageUrl());
            item.setNum(num);
            item.setPrice(detail.getRealPrice());
            item.setTotalFee(detail.getRealPrice() * num);
            item.setStorage(detail.getStorage() == null ? "快递" : detail.getStorage());
            item.setType(detail.getType());
            super.save(item);
        } else {
            item.setNum(item.getNum() + num);
            item.setTotalFee(item.getPrice() * item.getNum());
            super.update(item);
        }
        return item;
    }

    /**
     * 会员的购物车
     * @param memberId
     * @return
     */
    public List<CarItem> getMemberCars(Long memberId, Integer type){
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(3);
        param.put("type", type);
        param.put("memberId", memberId);
        param.put("deleted", false);
        List<CarItem> list = super.list(param);
        return list;
    }

    /**
     * 查找会员在购物车中某一个产品的记录
     * @param memberId
     * @param productId
     * @return
     */
    public CarItem getByMemberIdAndProductId(Long memberId, Long productId){
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(3);
        param.put("memberId", memberId);
        param.put("productId", productId);
        param.put("deleted", false);
        List<CarItem> list = super.list(param);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    /**
     * 删除购物车中的产品
     * @param ids
     */
    public void delete(List<Long> ids){
        List<CarItem> list = getByIds(ids);
        for (CarItem carItem :
                list) {
            carItem.setDeleted(true);
        }
        batchUpdate(list);
    }

    /**
     * 修改购物车中产品的数量
     * @param carId
     * @param num
     */
    public void updateNum(Long carId, int num){
        if(num == 0){//数量等于0  直接删除
            delete(carId);
        } else {
            CarItem item = getById(carId);
            item.setNum(num);
            update(item);
        }
    }

}
